import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-04
 * Time: 10:45
 */

//逆波兰表达式法
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x : tokens) {
            if(!isOperation(x)) {
                //是数字
                stack.push(Integer.parseInt(x));
            }else {
                //符号
                int ret2 = stack.pop();
                int ret1 = stack.pop();
                switch(x) {
                    case"+":
                        stack.push(ret1 + ret2);
                        break;
                    case"-":
                        stack.push(ret1 - ret2);
                        break;
                    case"*":
                        stack.push(ret1 * ret2);
                        break;
                    case"/":
                        stack.push(ret1 / ret2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String s) {
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }
}

public class Test1 {
}
